package com.seer.auto.controller;

import com.seer.auto.dao.DaoManager;
import com.seer.auto.dto.Pageable;
import com.seer.auto.dto.Response;
import java.util.List;
import java.util.Map;
import javassist.CannotCompileException;
import javassist.NotFoundException;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping
@ResponseBody
public class MainController {

    @Autowired
    private DaoManager daoManager;

    @PostMapping("/list/{model}")
    public Response<List<?>> list(@PathVariable(name = "model") String model,
        @RequestBody(required = false) Pageable p)
        throws Exception {
        int page = 1, size = 20;
        if (null != p) {
            page = p.getPageNumber();
            size = p.getPageSize();
        }
        return Response.ok(daoManager.load(model).list(page, size))
            .page(page, size, daoManager.load(model).count());
    }

    @GetMapping("/get/{model}/{id}")
    public Response<?> get(@PathVariable(name = "model") String model,
        @PathVariable(name = "id") Long id)
        throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {
        return Response.ok(daoManager.load(model).get(id));
    }

    @PutMapping("/new/{model}")
    public Response<?> create(@PathVariable(name = "model") String model, @RequestBody
        Map<String, Object> params)
        throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {
        return Response.ok(
            daoManager.load(model).create(daoManager.getEntityManager().create(model, params)));
    }

    @SneakyThrows
    @PostMapping("/edit/{model}/{id}")
    public Response<?> edit(@PathVariable(name = "model") String model,
        @RequestBody Map<String, Object> params,
        @PathVariable(name = "id") Long id)
        throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {
        return Response.ok(
            daoManager.load(model).update(id, daoManager.getEntityManager().create(model, params)));
    }

    @DeleteMapping("/delete/{model}/{id}")
    public Response<?> delete(@PathVariable(name = "model") String model,
        @PathVariable(name = "id") Long id)
        throws NotFoundException, CannotCompileException, InstantiationException, IllegalAccessException {
        return Response.ok(daoManager.load(model).delete(id));
    }
}
